package com.study.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.common.utils.R;
import com.study.modules.sys.model.dto.SysRoleDTO;
import com.study.modules.sys.model.entity.SysRole;
import com.study.modules.sys.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户
 *
 * @Author Mr.Du
 * @Date 2021/10/16 8:59
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 获取角色列表
     *
     * @return 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:role:list")
    public R selectList(Page<SysRole> page) {
        return R.ok().setData(roleService.queryRoleList(page));
    }

    /**
     * 查看角色详情
     *
     * @param roleId 角色id
     * @return 角色信息及绑定的菜单信息
     */
    @GetMapping("/{roleId}")
    @RequiresPermissions("sys:role:info")
    public R selectRoleInfo(@PathVariable("roleId") Long roleId) {
        return R.ok().setData(roleService.selectRoleInfo(roleId));
    }

    /**
     * 新增角色
     *
     * @param sysRole 角色信息
     * @return true
     */
    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    public R save(@RequestBody @Validated SysRoleDTO sysRole) {
        return R.ok().setData(roleService.insert(sysRole, getUserId()));
    }

    /**
     * 修改角色
     *
     * @param sysRole 角色信息和菜单id信息
     * @return true
     */
    @PutMapping
    @RequiresPermissions("sys:role:update")
    public R update(@RequestBody @Validated SysRoleDTO sysRole) {
        return R.ok().setData(roleService.updateRole(sysRole, getUserId()));
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     * @return true
     */
    @DeleteMapping("/{roleId}")
    @RequiresPermissions("sys:role:delete")
    public R delete(@PathVariable("roleId") Long roleId) {
        return R.ok().setData(roleService.delete(roleId, getUserId()));
    }

}
